package com.example.projectmanagement.service.study;

import com.example.projectmanagement.model.study.VideoAccessControl;
import com.example.projectmanagement.model.study.Lesson;
import com.example.projectmanagement.model.User;
import com.example.projectmanagement.repository.study.VideoAccessControlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class VideoAccessControlService {
    
    @Autowired
    private VideoAccessControlRepository videoAccessControlRepository;
    
    /**
     * 为视频添加学生访问权限
     */
    public VideoAccessControl addStudentAccess(Lesson video, User student, User lecturer) {
        // 检查是否已存在访问控制记录
        Optional<VideoAccessControl> existing = videoAccessControlRepository.findByVideoAndStudent(video, student);
        if (existing.isPresent()) {
            VideoAccessControl control = existing.get();
            control.setCanView(true);
            return videoAccessControlRepository.save(control);
        }
        
        // 创建新的访问控制记录
        VideoAccessControl control = new VideoAccessControl();
        control.setVideo(video);
        control.setStudent(student);
        control.setLecturer(lecturer);
        control.setCanView(true);
        control.setCreatedAt(java.time.LocalDateTime.now());
        
        return videoAccessControlRepository.save(control);
    }
    
    /**
     * 移除学生访问权限
     */
    public void removeStudentAccess(Lesson video, User student) {
        Optional<VideoAccessControl> existing = videoAccessControlRepository.findByVideoAndStudent(video, student);
        if (existing.isPresent()) {
            videoAccessControlRepository.delete(existing.get());
        }
    }
    
    /**
     * 检查学生是否有访问权限
     */
    public boolean hasStudentAccess(Lesson video, User student) {
        Optional<VideoAccessControl> existing = videoAccessControlRepository.findByVideoAndStudent(video, student);
        return existing.isPresent() && existing.get().getCanView();
    }
    
    /**
     * 获取视频的所有学生访问权限
     */
    public List<VideoAccessControl> getVideoAccessControls(Lesson video) {
        return videoAccessControlRepository.findByVideo(video);
    }
    
    /**
     * 获取学生的所有视频访问权限
     */
    public List<VideoAccessControl> getStudentAccessControls(User student) {
        return videoAccessControlRepository.findByStudent(student);
    }
    
    /**
     * 更新访问权限
     */
    public VideoAccessControl updateAccessPermission(Lesson video, User student, Boolean canView) {
        Optional<VideoAccessControl> existing = videoAccessControlRepository.findByVideoAndStudent(video, student);
        if (existing.isPresent()) {
            VideoAccessControl control = existing.get();
            control.setCanView(canView);
            return videoAccessControlRepository.save(control);
        }
        return null;
    }
}